import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:kuwo_music/pages/components/common_search_header.dart';
import 'package:kuwo_music/pages/components/index_drawer.dart';
import 'package:kuwo_music/pages/components/rotateSong.dart';
import 'package:kuwo_music/pages/home/home_live/index.dart';
import 'package:kuwo_music/pages/home/home_my/index.dart';
import 'package:kuwo_music/pages/home/home_song/index.dart';
import 'package:kuwo_music/provider/module/common.dart';
import 'package:provider/provider.dart';

import 'home_index/index.dart';

class BarItem {
  final Widget title;
  final Widget icon;
  final Widget activeIcon;
  final Function onTap;
  final Widget widget;
  BarItem({this.title, this.icon, this.activeIcon, this.onTap, this.widget});
}

List<BarItem> barItemList = [
  BarItem(
      title: Text('首页'),
      icon: Icon(Icons.home_outlined),
      activeIcon: Icon(Icons.home)),
  BarItem(
      title: Text('曲库'),
      icon: Icon(Icons.search),
      activeIcon: Icon(Icons.search, fontWeight: FontWeight.bold)),
  BarItem(widget: RotateSong(), onTap: playMusic),
  BarItem(
      title: Text('直播'),
      icon: Icon(Icons.videocam_outlined),
      activeIcon: Icon(Icons.videocam)),
  BarItem(
      title: Text('我的'),
      icon: Icon(Icons.account_circle_outlined),
      activeIcon: Icon(Icons.account_circle)),
];

playMusic(context) {
  // AudioPlayer.logEnabled = true;
  AudioPlayer audioPlayer =
      Provider.of<CommonInfo>(context, listen: false).audioPlayer;
  AudioPlayerState audioPlayerState =
      Provider.of<CommonInfo>(context, listen: false).audioPlayerState;
  if (audioPlayerState == null) {
    audioPlayer.play('http://localhost:9999/static/test.mp3');
  } else if (audioPlayerState == AudioPlayerState.PLAYING) {
    audioPlayer.pause();
  } else {
    audioPlayer.resume();
  }
}

List<Widget> bodyList = [
  HomeIndexPage(),
  HomeSongPage(),
  null,
  HomeLivePage(),
  HomeMyPage(),
];

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  int _selectedIndex = 0;
  AnimationController _animationController;
  Animation _animation;

  @override
  void initState() {
    super.initState();
    _animationController =
        AnimationController(vsync: this, duration: Duration(milliseconds: 500));
    _animation = Tween(begin: 0.0, end: .125).animate(_animationController);

    // _animationController.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        // leading: Text('抽屉'),
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.max,
          children: [
            Expanded(
              child: Container(
                height: 30,
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(20))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.search,
                      color: Colors.grey[300],
                      size: 14.0,
                    ),
                    Text('永不失联的哎',
                        style:
                            TextStyle(color: Colors.grey[300], fontSize: 14.0))
                  ],
                ),
              ),
            )
          ],
        ),
        actions: [
          GestureDetector(
            onTap: () async {
              _animationController.forward();
              await showMenu(
                  context: context,
                  position: RelativeRect.fromSize(
                      Rect.fromLTWH(10, 100, 10, 10), Size.square(1)),
                  items: <PopupMenuEntry>[
                    buildPopupMenuItem(Icon(Icons.music_note_rounded), '听歌识曲'),
                    PopupMenuDivider(),
                    buildPopupMenuItem(Icon(Icons.music_note_rounded), '最近播放'),
                    PopupMenuDivider(),
                    buildPopupMenuItem(Icon(Icons.music_note_rounded), '扫一扫 '),
                  ]);
              _animationController.reverse();
            },
            child: Container(
                width: kToolbarHeight,
                child: RotationTransition(
                    turns: _animation, child: Icon(Icons.add_circle_outline))),
          )
        ],
      ),
      drawer: IndexDrawer(),
      body: bodyList[_selectedIndex],
      bottomNavigationBar: BottomAppBar(
        child: DefaultTextStyle(
          style: TextStyle(fontSize: 14, color: Colors.black),
          child: Container(
            height: 60,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: barItemList.asMap().keys.map((int index) {
                BarItem e = barItemList[index];
                if (e.widget != null) {
                  // return Expanded(child: e.widget);
                  return GestureDetector(
                    child: e.widget,
                    onTap: () => {e.onTap(context)},
                  );
                } else {
                  if (_selectedIndex == index) {
                    return Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [e.activeIcon, e.title],
                      ),
                    );
                  } else {
                    return Expanded(
                      child: GestureDetector(
                        onTap: () {
                          setState(() {
                            _selectedIndex = index;
                          });
                        },
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [e.icon, e.title],
                        ),
                      ),
                    );
                  }
                }
              }).toList(),
            ),
          ),
        ),
      ),
      floatingActionButtonLocation:
          FloatingActionButtonLocation.miniCenterFloat,
    );
  }

  PopupMenuItem buildPopupMenuItem(
    Widget icon,
    String text,
  ) {
    return PopupMenuItem(
        child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        icon,
        Container(
          margin: EdgeInsets.only(left: 10),
          child: Text(
            text,
            textAlign: TextAlign.center,
          ),
          width: 70,
        ),
      ],
    ));
  }
}
